/**********************************************
 *mail:hebinq@qq.com
 *    -lmysqlclient -std=c++11
 *
 * *******************************************/
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>      
#include <mysql/mysql.h>  
#include <jsoncpp/json/json.h>
#include "myDB.h"
#include <ctime>

using namespace std;

MyDB::MyDB()     
{
	mysql = mysql_init(NULL);
     if(mysql == NULL)
     {
   cout << "Error: " << mysql_error(mysql);
	exit(-1);
	}           
}
MyDB::~MyDB()
{                                                                                                                     
     if(!mysql)
     {
   mysql_close(mysql);
     }
}
bool MyDB::InitDB(string host,string user,string pwd,string dbname)
{
	/*连接数据库*/
	if(mysql==NULL) return false;
     if(!mysql_real_connect(mysql,host.c_str(),user.c_str(),pwd.c_str(),dbname.c_str(),0,NULL,0))
     {
        cout << "connect fial: " << mysql_error(mysql);
        return false;
     }
     return true;
}

int MyDB::rowCount(string sql)
{
	if(mysql==NULL) return -1;
	int ret=-1;
     /*执行失败*/
	if(mysql_query(mysql,sql.c_str()))
     {
        cout << "query fail: " << mysql_error(mysql);
     }
	else
	{
		result=mysql_store_result(mysql);
		ret=mysql_num_rows(result);
	}
	return ret;
}
int MyDB::ExeSQL(string sql)
{
	if(mysql==NULL) return -1;
	int ret=-1;
     /*执行失败*/
	if(mysql_query(mysql,sql.c_str()))
     {
        cout << "exeSQL fail: " << mysql_error(mysql);
     }
	else
	{
        my_ulonglong affected_row = mysql_affected_rows(mysql);		
		ret=(int)affected_row;
	}
	return ret;
}
bool MyDB::resultSQL(string sql,Json::Value &resultArray)
{
	if(mysql==NULL) return false;
	if(mysql_query(mysql,sql.c_str()))
	{
		cout<<"query fail:"<<mysql_error(mysql);
		return false;
	}
	else
     {
	     
   /*获取结果集*/
            result = mysql_store_result(mysql);
              if(result == NULL)
                    return false;

              Json::Value arrayField;
             for(int i=0; i<mysql_num_fields(result); i++)
             {
                 field = mysql_fetch_field_direct(result,i);
                 Json::Value item;
                 item["index"]=i;
                 item["name"]=field->name;
                 item["type"]=field->type;
                 item["table"]=field->table;
                 item["max_length"]=(Json::LargestUInt)field->max_length;
                 arrayField.append(item);
                //cout << field->name << "\t";
             }
             resultArray["fields"]=arrayField;
             //cout << endl;

             row = mysql_fetch_row(result);
             Json::Value arrayRow;
             while(row != NULL)
             {
                 Json::Value itemRow;
               for(int i=0; i<mysql_num_fields(result); i++)
                {
                   field = mysql_fetch_field_direct(result,i);
                   //Json::Value valitem;
                   string colname=field->name;
                   if(row[i]!=NULL)
                   {
                       if(field->type==FIELD_TYPE_DECIMAL) itemRow[colname]=atof(row[i]);
                       else if(field->type==FIELD_TYPE_NEWDECIMAL) itemRow[colname]=atof(row[i]);
                       else if(field->type==FIELD_TYPE_TINY) itemRow[colname]=atoi(row[i]);
                       else if(field->type==FIELD_TYPE_SHORT) itemRow[colname]=atoi(row[i]);
                       else if(field->type==FIELD_TYPE_LONG) itemRow[colname]=atoi(row[i]);
                       else if(field->type==FIELD_TYPE_FLOAT) itemRow[colname]=atof(row[i]);
                       else if(field->type==FIELD_TYPE_DOUBLE) itemRow[colname]=atof(row[i]);
                       else
                       itemRow[colname]=row[i];
                   }
                   else
                       itemRow[colname]=Json::nullValue;
                    //cout << row[i] << "\t";
                    //itemRow.append(valitem);
                }
                //cout << endl;
                arrayRow.append(itemRow);
                row = mysql_fetch_row(result);
             }
             resultArray["rows"]=arrayRow;
            cout<<endl;
     }
	return true;
} 
